﻿using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc;
using Nop.Core.Data;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Data;
using Nop.Plugin.Pricing.PreciousMetals.Data;
using Nop.Plugin.Pricing.PreciousMetals.Domain;
using Nop.Plugin.Pricing.PreciousMetals.Services;
using Nop.Services.Catalog;
using Nop.Services.Orders;

namespace Nop.Plugin.Pricing.PreciousMetals
{
    public class DependencyRegistrar : IDependencyRegistrar
    {
        public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            //data layer
            var dataSettingsManager = new DataSettingsManager();
            var dataProviderSettings = dataSettingsManager.LoadSettings();

            const string objectContextName = "nop_object_context_preciousmetals_pricing";

            if (dataProviderSettings != null && dataProviderSettings.IsValid())
            {
                //register named context
                builder.Register<IDbContext>(c => new PreciousMetalsPricingObjectContext(dataProviderSettings.DataConnectionString))
                    .Named<IDbContext>(objectContextName)
                    .InstancePerHttpRequest();

                builder.Register(c => new PreciousMetalsPricingObjectContext(dataProviderSettings.DataConnectionString))
                    .InstancePerHttpRequest();
            }
            else
            {
                //register named context
                builder.Register<IDbContext>(c => new PreciousMetalsPricingObjectContext(c.Resolve<DataSettings>().DataConnectionString))
                    .Named<IDbContext>(objectContextName)
                    .InstancePerHttpRequest();

                builder.Register(c => new PreciousMetalsPricingObjectContext(c.Resolve<DataSettings>().DataConnectionString))
                    .InstancePerHttpRequest();
            }

            // register custom objects
            builder.RegisterType<EfRepository<PreciousMetalsDetail>>()
                .As<IRepository<PreciousMetalsDetail>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>(objectContextName))
                .InstancePerHttpRequest();

            builder.RegisterType<EfRepository<PreciousMetalsQuote>>()
                .As<IRepository<PreciousMetalsQuote>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>(objectContextName))
                .InstancePerHttpRequest();
            
            
            //Register services
            builder.RegisterType<PreciousMetalsDetailService>().As<IPreciousMetalsDetailService>().InstancePerHttpRequest();
            builder.RegisterType<PreciousMetalsQuoteService>().As<IPreciousMetalsQuoteService>().InstancePerHttpRequest();
            builder.RegisterType<PreciousMetalsPriceCalculationService>().As<IPriceCalculationService>().InstancePerHttpRequest();
            builder.RegisterType<PreciousMetalsOrderTotalCalculationService>().As<IOrderTotalCalculationService>().InstancePerHttpRequest();
            builder.RegisterType<PreciousMetalsProductService>().As<IProductService>().InstancePerHttpRequest();

        }

        public int Order
        {
            get { return 1; }
        }
    }
}
